package niuke.week01;

import java.util.Scanner;

/**
 输入一个整数 n ，求1～n这n个整数的十进制表示中1出现的次数
 例如，1~13中包含1的数字有1、10、11、12、13因此共出现6次
 */
public class NumberOf1Between1AndN {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number =  sc.nextInt();
        System.out.println(NumberOf1Between1AndN_Solution(number));

    }

    public static int NumberOf1Between1AndN_Solution(int number) {
        if (number==0)
            return 0;

        if(number<10)
            return  1;

        int k = (number + "").length();
        /**
         * 3765
         * 765---递归
         *766-3765---非递归
         */

        char statr = (number+"").charAt(0);
        int  tmp1= (int) Math.pow(10,k-1);
        int firstOneNum = statr == '1' ? number % tmp1 + 1 : tmp1;
        int otherOneNum = Integer.valueOf(statr+"") * (k - 1) * (tmp1 / 10);

        int a = NumberOf1Between1AndN_Solution(number % tmp1);//765---递归

        return firstOneNum + otherOneNum + a;

    }
}